package cn.com.nes.site.entity.em;

import cn.com.nes.common.exception.MissBusinessParameterException;
import cn.hutool.core.util.ObjectUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工单状态
 * @author lux
 */
public enum HandleTypeEnum {

    _0("0", "换件", false),

    _1("1", "换机器", false),

    _2("2", "维修", false),

    _3("3", "调试", true),

    _4("4", "无需处理", false),

    _5("5", "其他", false),

    ;

    private String key;

    private String val;

    @Getter
    private Boolean isComplete; // 当卡状态码对应的工单是否已安装完成

    HandleTypeEnum(String key, String val, Boolean isComplete) {
        this.key = key;
        this.val = val;
        this.isComplete = isComplete;
    }

    public String getVal() {
        return val;
    }

    public String getKey() {
        return key;
    }

    public static String getVal(Integer key) {
        Map<String, String> map = Arrays.stream(values())
                .collect(Collectors.toMap(HandleTypeEnum::getKey, HandleTypeEnum::getVal));
        return ObjectUtil.isNull(key) ? "" : map.get(key.toString());
    }

    // 获取安装完成状态对应值的列表
    public static List<String> getStatusList(Boolean isComplete) {
        if (ObjectUtil.isNull(isComplete)) {
            throw new MissBusinessParameterException("isComplete must not null");
        }

        return Arrays.stream(values())
                .filter(n -> isComplete.equals(n.getIsComplete()))
                .map(HandleTypeEnum::getKey)
                .collect(Collectors.toList());
    }

    // 按状态获取工单是否已完成
    public static Boolean getIsComplete(Integer key) {
        Map<String, Boolean> map = Arrays.stream(values())
                .collect(Collectors.toMap(HandleTypeEnum::getKey, HandleTypeEnum::getIsComplete));
        return !ObjectUtil.isNull(key) && map.get(key.toString());
    }

}
